using System.Collections;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using GameStateManagement.Model.Entities.Interfaces;

namespace GameStateManagement.Model.Entities
{
    /// <summary>
    /// Composite pattern for IEntity, which EntityBase implements and that makes all entities implement it.
    /// </summary>
    public class CompositeEntity : IEntity, IEnumerable<IEntity>
    {
        public List<IEntity> Childs;

        public CompositeEntity()
        {
            Childs = new List<IEntity>();
        }

        public void Update(GameTime gameTime)
        {
            foreach (IEntity child in Childs)
            {
                child.Update(gameTime);
            }
        }

        public void AddChild(IEntity entity)
        {
            Childs.Add(entity);
        }

        public int Count { get { return Childs.Count; } }

        #region IEnumerable
        public IEnumerator<IEntity> GetEnumerator()
        {
            return Childs.GetEnumerator();
        }

        public IEntity[] GetArray()
        {
            return Childs.ToArray();
        }

        
        IEnumerator IEnumerable.GetEnumerator()
        {
            return Childs.GetEnumerator();
        }
        #endregion
    }
}
